/**
*@ClassName: SysOssController
*@Description
*@Author maozhi
*@Date 2021/3/22
*@Time 14:49
*/

package com.universityJob.modules.oss.controller;

import com.google.gson.Gson;
import com.universityJob.common.exception.RRException;
import com.universityJob.common.utils.*;
import com.universityJob.modules.oss.service.SysOssService;
import com.universityJob.modules.sys.service.SysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传
 *
 *
 */
@RestController
@RequestMapping("sys/oss")
public class SysOssController {
	@Autowired
	private SysOssService sysOssService;
    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
	private Upload upload;

    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:oss:all")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysOssService.queryPage(params);

		return R.ok().put("page", page);
	}



	/**
	 * 上传文件
	 */
	@PostMapping("/upload")
	@RequiresPermissions("sys:oss:all")
	public R upload(@RequestParam("file") MultipartFile file,@RequestParam("fileName") String fileName) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		if (fileName == null || "".equals(fileName)){
			return R.error();
		}
		R r = upload.uploadFile(file,fileName);
		return r;
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody String path){
		boolean b = upload.deleteFile(path);
		return R.ok();
	}

}
